package com.example.trtcdemo;

import static com.tencent.trtc.TRTCCloudDef.TRTC_APP_SCENE_LIVE;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tencent.trtc.TRTCCloud;
import com.tencent.trtc.TRTCCloudDef;

public class MainActivity extends TRTCBaseActivity implements View.OnClickListener {

    private EditText etRoomId,etUserId;
    private Button btEnterRoom, btAnchor, btAudience;
    private String mUserId,mRoomId;
    private int mRoleSelectFlag = 1;//角色标识 默认主播
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().hide();
        initView();
        initListener();
    }

    private void initListener() {
        btEnterRoom.setOnClickListener(this);
        btAnchor.setOnClickListener(this);
        btAudience.setOnClickListener(this);
    }

    private void initView() {
        etRoomId = findViewById(R.id.et_input_room_id);
        etUserId = findViewById(R.id.et_input_username);
        btEnterRoom = findViewById(R.id.bt_enter_room);
        btAnchor = findViewById(R.id.btn_anchor);
        btAudience = findViewById(R.id.btn_audience);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_enter_room:
                if (checkPermission()){
                    if (1 == mRoleSelectFlag){
                        startEnterRoom();
                    }else if (2 == mRoleSelectFlag){
                        playCDNStream();
                    }

                }
                break;
            case R.id.btn_anchor:
                if(mRoleSelectFlag != 1){
                    mRoleSelectFlag = 1;
                    btAnchor.setBackgroundColor(getResources().getColor(R.color.live_single_select_button_bg));
                    btAudience.setBackgroundColor(getResources().getColor(R.color.live_single_select_button_bg_off));
                }
                break;
            case R.id.btn_audience:
                if(mRoleSelectFlag != 2){
                    mRoleSelectFlag = 2;
                    btAnchor.setBackgroundColor(getResources().getColor(R.color.live_single_select_button_bg_off));
                    btAudience.setBackgroundColor(getResources().getColor(R.color.live_single_select_button_bg));
                }
                break;
        }
    }

    private void startEnterRoom() {
        mRoomId = etRoomId.getText().toString().trim();
        mUserId = etUserId.getText().toString().trim();
        if (TextUtils.isEmpty(mRoomId)
                || TextUtils.isEmpty(mUserId)) {
            Toast.makeText(MainActivity.this, getString(R.string.live_room_input_error_tip), Toast.LENGTH_LONG).show();
            return;
        }
        Intent intent = new Intent(MainActivity.this, LiveActivity.class);

        intent.putExtra(Constant.ROOM_ID, mRoomId);
        intent.putExtra(Constant.USER_ID, mUserId);
        startActivity(intent);

    }

    private void playCDNStream(){
        Intent intent = new Intent(MainActivity.this, PushCDNAudienceActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onPermissionGranted() {
        startEnterRoom();
    }
}